package com.mooc.mymooc.controller;

import com.mooc.mymooc.pojo.Teacher;
import com.mooc.mymooc.pojo.UserFavorite;
import com.mooc.mymooc.service.MoocService;
import com.mooc.mymooc.utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller

public class TeacherController {
    @Autowired
    private MoocService moocService;

    /**
     * 展示教师列表
     * @return
     */
    @RequestMapping(value = "/showTeacherList")
    public String showTeacherList(Model model,String sort,Integer pageIndex){
        Integer countRecord = this.moocService.countTeachers();
        PageModel pageModel = new PageModel();
        pageModel.setRecordCount(countRecord);
        pageModel.setPageSize(5);
        if (pageIndex!=null){
            pageModel.setPageIndex(pageIndex);
        }
        List<Teacher> teacherList = this.moocService.findAllTeacher(pageModel,sort);
        List<Teacher> teachers = this.moocService.findAllTeacher(null,"fav_nums");
        model.addAttribute("teachers",teacherList);
        model.addAttribute("pageModel",pageModel);
        model.addAttribute("sort",sort);
        model.addAttribute("right_teachers",teachers);
        return "teachers-list";
    }

    @RequestMapping(value = "/teacher_detail")
    public String teacher_detail(Integer id, Model model, HttpSession session){
        Teacher teacher = this.moocService.findTeacherById(id);
        this.moocService.addClickNums_teacher(id);
        model.addAttribute("teacher",teacher);
        UserFavorite userFavorite = new UserFavorite();
        userFavorite.setFav_id(id);
        userFavorite.setFav_type(1);
        UserFavorite userFavorite1 = this.moocService.findIsFavIndex(userFavorite);
        UserFavorite userFavorite2 = new UserFavorite();
        userFavorite2.setFav_id(id);
        userFavorite2.setFav_type(2);
        UserFavorite userFavorite3 = this.moocService.findIsFavIndex(userFavorite2);
        if (userFavorite1!=null && session.getAttribute("user_session")!=null){
            model.addAttribute("favflag","yes");
        }else{
            model.addAttribute("favflag","no");
        }
        if (userFavorite3!=null && session.getAttribute("user_session")!=null){
            model.addAttribute("favflag1","yes");
        }else{
            model.addAttribute("favflag1","no");
        }
        return "teacher-detail";
    }
}
